package com.hs;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: hs
 * @Description:
 * @Date: Create in 10:44 2019/4/22
 */
public class CountDownLatchTest extends Thread{
    CountDownLatch countDownLatch;
    public CountDownLatchTest(CountDownLatch countDownLatch){
        this.countDownLatch =countDownLatch;
    }

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(2);
        CountDownLatchTest thread1  = new CountDownLatchTest(countDownLatch);
        CountDownLatchTest thread2  = new CountDownLatchTest(countDownLatch);
        try {
            thread1.start();
            thread2.start();
            System.out.println("waiting");
            countDownLatch.await();
            System.out.println("end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run(){
        System.out.println(Thread.currentThread().getContextClassLoader());
        System.out.println(Thread.currentThread().getName());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        countDownLatch.countDown();
    }
}
